//
//  PaperDetailViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "PaperDetailViewController.h"

@interface PaperDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)UIButton *backBtn;
@property (nonatomic, strong)UIButton *loadBtn;
@property (nonatomic, strong)UIButton *collectBtn;
@property (nonatomic, strong)UIButton *shareBtn;
@property (nonatomic,strong) UIScrollView *scollerView;

@property (nonatomic, strong)UIActivityIndicatorView *act;
@end

@implementation PaperDetailViewController
@synthesize isTap;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _scollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _scollerView.contentSize = CGSizeMake(self.ScollreArr.count*kWidth, 0);
    _scollerView.contentOffset = CGPointMake(self.number*kWidth, 0);
    _scollerView.pagingEnabled = YES;
    _scollerView.delegate = self;
    _scollerView.tag = 100000;
    _scollerView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scollerView];
    
    
    for (int i = 0; i < self.ScollreArr.count; i++) {
       UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0 + i*kWidth, -64, kWidth,kHeight)];
        scrollView.tag = 2100+i;
        [_scollerView addSubview:scrollView];
        scrollView.delegate = self;
        //缩小的最小比例(按倍数变化)
        scrollView.minimumZoomScale = 0.5;
        //放大的最大比例(按倍数变化)
        scrollView.maximumZoomScale = 3.0;
        UIImageView *image = [[UIImageView alloc] initWithFrame:kBounds];
        image.userInteractionEnabled = YES;
        
        //给背景加手势
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];
        
        [gesture setNumberOfTouchesRequired:1];
        
        [image addGestureRecognizer:gesture];
        
        image.tag = 2200+i;
        FortunePaperModel  * modle = self.ScollreArr[i];
        
        [image sd_setImageWithURL:[NSURL URLWithString:modle.path]];
          [scrollView addSubview:image];
        
    }
    [self setButton];
  
    self.isTap = YES;
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"设为背景" style:(UIBarButtonItemStyleDone) target:self action:@selector(backGrond:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
   
   }

- (void)backGrond:(UIBarButtonItem *)btn{
      FortunePaperModel  * modle = self.ScollreArr[self.number];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:modle.path]];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"picture"];
    //显示alertView提示用户
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改完成" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [alertView show];
    
    //0.3秒后alertView消失
    [self performSelector:@selector(p_removeAlertView:) withObject:alertView afterDelay:0.3];
}
- (void)p_removeAlertView:(UIAlertView*)aler{
    [aler dismissWithClickedButtonIndex:0 animated:YES];
}
//ImageView的点击方法
- (void)tapImage {
    if (self.isTap == YES) {
     [UIView animateWithDuration:0.2 animations:^{
         self.navigationController.navigationBar.hidden = YES;
         self.backBtn.hidden = YES;
         self.loadBtn.hidden = YES;
         self.collectBtn.hidden = YES;
         self.shareBtn.hidden = YES;
     } completion:nil];
    self.isTap = NO;
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.navigationController.navigationBar.hidden = NO;
            self.backBtn.hidden = NO;
            self.loadBtn.hidden = NO;
            self.collectBtn.hidden = NO;
            self.shareBtn.hidden = NO;
        }];
        self.isTap = YES;
    }
}
- (void)setButton{
    
    self.backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.backBtn setImage:[UIImage imageNamed:@"btn1.png"] forState:(UIControlStateNormal)];
    self.backBtn.frame = CGRectMake((kWidth - 20)/5, kHeight - 80, 50 , 50);
    
    self.loadBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.loadBtn addTarget:self action:@selector(loadBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.loadBtn setImage:[UIImage imageNamed:@"btn2.png"] forState:(UIControlStateNormal)];
    self.loadBtn.frame = CGRectMake(self.backBtn.frame.origin.x + 60, kHeight - 80, 50, 50);
 
    
    self.collectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.collectBtn addTarget:self action:@selector(collectBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.collectBtn setImage:[UIImage imageNamed:@"btn3.png"] forState:(UIControlStateNormal)];
    self.collectBtn.frame = CGRectMake(self.loadBtn.frame.origin.x + 60, kHeight - 80, 50, 50);

    
    self.shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.shareBtn setImage:[UIImage imageNamed:@"btn4.png"] forState:(UIControlStateNormal)];
    self.shareBtn.frame = CGRectMake(self.collectBtn.frame.origin.x + 60, kHeight - 80, 50, 50);
  

    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.loadBtn];
    [self.view addSubview:self.collectBtn];
    [self.view addSubview:self.shareBtn];
}

//返回按钮
- (void)backBtnClick{
    [self transitionWithType:@"rippleEffect" WithSubtype:kCATransitionFromRight ];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma CATransition动画实现
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = 0.7f;
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
    //设置子类
    animation.subtype = subtype;
    }
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [self.navigationController.view.layer addAnimation:animation forKey:@"animation"];
}
//保存按钮
- (void)loadBtnClick{
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"保存至系统相册" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    aler.delegate = self;
    [aler show];
}
//保存按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  int a = self.scollerView.contentOffset.x/kWidth;
   FortunePaperModel *model =  self.ScollreArr[a];
    NSURL *url = [NSURL URLWithString:model.path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    if (buttonIndex == 0) {
        self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
        self.act.center = self.view.center;
        [self.view addSubview:self.act];
        [self.act startAnimating];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        dispatch_async(dispatch_get_main_queue(),^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"存储成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [self.act stopAnimating];
            });
        });
    }else{
        return;
    }
}
//收藏按钮
- (void)collectBtnClick{
  UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [alertView show];
    [[PlayerDataBase shareDataBase] createTables];
    [[PlayerDataBase shareDataBase] insertFortunePaperModel:self.model];
    //0.3秒后alertView消失
    [self performSelector:@selector(RemoveAlertAndAct:) withObject:alertView afterDelay:0.3];
    }
//alert消失
- (void)RemoveAlertAndAct:(UIAlertView *)alertView{
 [alertView dismissWithClickedButtonIndex:0 animated:YES];
}
//分享按钮
- (void)shareBtnClick{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    if (name!=nil) {
        NSURL *url = [NSURL URLWithString:self.model.path];
        NSData *data = [NSData dataWithContentsOfURL:url];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5604fd14e0f55abb670003f8" shareText:nil shareImage:[UIImage imageWithData:data] shareToSnsNames:[NSArray arrayWithObjects:UMShareToDouban,UMShareToRenren,UMShareToSina,UMShareToTencent, nil] delegate:self];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        [[weatherHandle shareWetherHandle]p_removeAlertView:alert];
        [[weatherHandle shareWetherHandle] pleasedLoded:self];
    }
}
//缩放设置
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    NSArray *arr = [scrollView subviews];
    UIImageView *imageView = arr[0];
    return imageView;
}
//结束缩放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (int i = 2100; i < self.ScollreArr.count + 2100; i++) {
        UIScrollView *subScrollow = (UIScrollView *)[self.view viewWithTag:i];
        [subScrollow setZoomScale:1 animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
